#include "common.hpp"

int main()
{
  int shmid = createShm();
  char *shmaddr = (char *)shmat(shmid, nullptr, 0);
  // IPC通信
  while (true)
  {
    cout << "client say@ " << shmaddr << endl;
    sleep(1);
  }

  shmdt(shmaddr);
  shmctl(shmid, IPC_RMID, nullptr);

  // sleep(3);
  // // 让操作系统创建共享内存
  // int shmid = createShm();

  // sleep(3);
  // // 把共享内存挂接到进程地址空间
  // char *shmaddr = (char *)shmat(shmid, nullptr, 0);
  // if (shmaddr == (char *)-1)
  // {
  //   log(Fatal, "shmat is failed, error: %s", strerror(errno));
  //   exit(3);
  // }
  // log(Info, "shmat is successful, shmaddr: %s", shmaddr);
  // sleep(3);

  // // 取消挂接
  // int dt = shmdt(shmaddr);
  // if (dt < 0)
  // {
  //   log(Error, "shmdt dttach is failed, error: %s", strerror(errno));
  //   exit(4);
  // }
  // log(Info, "shmdt dttch is successful, dt: %d", dt);

  // sleep(3);

  // // 释放共享内存
  // int del = shmctl(shmid, IPC_RMID, nullptr);
  // if (del < 0)
  // {
  //   log(Error, "shmctl destroy is failed, error: %s", strerror(errno));
  //   exit(5);
  // }
  // log(Info, "shmctl destroy is successful, del: %d", del);

  // sleep(3);

  return 0;
}